<template>
    <div style="">
        <Header :left="true" leftText="返回" title="异常让步质量申请审核"> </Header>
        <NoticeBar color="#1989fa" background="#ecf9ff" style="margin-top: 5px; font-size: 15px; font-weight: bold"
            left-icon="description">异常让步质量申请</NoticeBar>
        <van-form :style="autoHeight" @submit="onSubmit">

            <van-field readonly class="formstyle" style="font-weight: bold" name="picker" :value="showOrder" label="生产订单"
                placeholder="点击选择生产订单" show-word-limit type="text"
                v-if="this.form.POrderID !== null && this.form.POrderID !== '' && this.form.POrderID !== undefined" rows="2"
                autosize />

            <van-field class="formstyle" readonly :value="form.MaterialID"
                v-if="this.form.POrderID !== null && this.form.POrderID !== '' && this.form.POrderID !== undefined"
                name="物料号" label="物料代码" placeholder="输入物料代码" />
            <van-field readonly class="formstyle" :value="form.MaterialName" name="物料名称" label="物料名称"
                v-if="this.form.POrderID !== null && this.form.POrderID !== '' && this.form.POrderID !== undefined"
                placeholder="输入物料名称" />

            <van-field readonly class="formstyle" required :value="showShift" name="班次" label="班次" placeholder="请选择班次"
                :rules="[{ required: true, message: '班次不能为空' }]" autosize />

            <van-field readonly required class="formstyle" :value="showFactory" name="工厂代码" label="工厂代码"
                placeholder="点击选择工厂代码" show-word-limit :rules="[{ required: true, message: '工厂代码不能为空' }]" rows="2"
                autosize />

            <van-field required readonly class="formstyle" :value="showMachine" name="设备代码" label="设备代码" show-word-limit
                :rules="[{ required: true, message: '设备代码不能为空' }]" rows="2" autosize placeholder="选择设备代码" />

            <van-field readonly required class="formstyle" v-model="showAbn" name="异常类型" label="异常类型" placeholder="选择异常类型"
                :rules="[{ required: true, message: '异常类型不能为空' }]" show-word-limit rows="2" autosize
                @click="showPickerNoticeMemo = true" />

            <van-field readonly required class="formstyle" v-model="showAbnplus" maxlength="500" type="textarea"
                name="质量异常原因" label="质量异常原因" placeholder="质量异常原因" rows="5" />

            <NoticeBar color="#1989fa" background="#ecf9ff" style="margin-top: 5px; font-size: 15px; font-weight: bold"
                left-icon="description">异常信息</NoticeBar>

            <van-field readonly class="formstyle" :value="form.CreateDatetime" name="异常发生时间" label="异常发生时间"
                placeholder="选择异常发生时间" show-word-limit :rules="[{ required: true, message: '异常发生时间不能为空' }]" rows="2"
                autosize />

            <van-field readonly class="formstyle" :value="form.CompleteDatetime" name="让步生产截止时间" label="让步生产截止时间"
                placeholder="让步生产截止时间" show-word-limit :rules="[{ required: true, message: '让步生产截止时间不能为空' }]"
                @click="showTimePicker = true" rows="2" autosize />

            <van-field required readonly class="formstyle" rows="4" autosize type="textarea" maxlength="100"
                v-model="form.RequestMemo" name="申请理由" label="申请理由" placeholder="输入申请理由"
                :rules="[{ required: true, message: '申请理由不能为空' }]" />

            <NoticeBar color="#1989fa" background="#ecf9ff" style="margin-top: 5px; font-size: 15px; font-weight: bold"
                left-icon="description">审核信息</NoticeBar>



            <van-field :name="type" :label="type + '审批'" required class="formstyle" :readonly="type2">
                <template #input>
                    <van-radio-group v-model="form.PrdAgree" direction="horizontal" :disabled="type2 || lock">
                        <van-radio :name="true">同意</van-radio>
                        <van-radio :name="false">不同意</van-radio>
                    </van-radio-group>
                </template>
            </van-field>

            <van-field required class="formstyle" rows="4" autosize type="textarea" maxlength="100"
                :readonly="type2 || lock" :rules="[{ required: true, message: '审核意见不能为空' }]" v-model="form.PrdCheckedMemo"
                name="审核意见" label="审核意见" />



            <NoticeBar color="#1989fa" background="#ecf9ff" style="margin-top: 5px; font-size: 15px; font-weight: bold"
                left-icon="description" v-if="type2">申请批准</NoticeBar>

            <van-field :name="type" label="利润点负责人审批" required class="formstyle" v-if="type2">
                <template #input>
                    <van-radio-group v-model="form.FactoryManagerAgree" direction="horizontal" :disabled="lock">
                        <van-radio :name="true">同意</van-radio>
                        <van-radio :name="false">不同意</van-radio>
                    </van-radio-group>
                </template>
            </van-field>

            <van-field required class="formstyle" rows="4" autosize type="textarea" maxlength="100" v-if="type2"
                :rules="[{ required: true, message: '审批意见不能为空' }]" :readonly="lock" v-model="form.FactoryManagerCheckedMemo"
                name="审批意见" label="审批意见" />

            <div style="margin: 16px" v-if="!lock">
                <van-button color="#138087" round block type="info" native-type="submit" @click="submitsure">保存</van-button>
            </div>
        </van-form>
    </div>
</template>
  
<script>
import {
    Col,
    Row,
    Form,
    Cell,
    Icon,
    Field,
    NumberKeyboard,
    Image as VanImage,
    Picker,
    popup,
    SubmitBar,
    Button,
    Loading,
    Overlay,
    Dialog,
    Toast,
    NoticeBar,
    Tag,
    DatetimePicker,
    ImagePreview,
    CellGroup,
    Uploader,
    Cascader,
    Calendar,
    RadioGroup,
    Radio,
    Divider,
    Stepper,
    AddressList,
    Checkbox,
    CheckboxGroup,

} from "vant";


import "vue-easytable/libs/themes-base/index.css";
import { VTable } from "vue-easytable";
import { _httpClient } from "@/libs/apisie";
import { _httpClient as _httpClientApi } from "@/libs/api";
import Header from "@/components/header.vue";
import { storage } from "@/libs/utils";


Vue.use(Form);
Vue.use(Field);
Vue.use(Overlay);
Vue.use(Loading);
Vue.use(Picker);
Vue.use(popup);
Vue.use(SubmitBar);
Vue.use(VanImage);
Vue.use(DatetimePicker);
Vue.use(Cascader);
Vue.use(Calendar);
Vue.use(Cell);
Vue.use(RadioGroup);
Vue.use(Radio);
Vue.use(Divider);
Vue.use(Stepper);
Vue.use(Button);
Vue.use(AddressList);
Vue.use(Checkbox);
Vue.use(CheckboxGroup);
Vue.use(Col);
Vue.use(Row);

export default {
    name: "abnormalconcessionAduit",
    components: {
        Form,
        Row,
        Col,
        Button,
        Header,
        Form,
        Cell,
        Icon,
        Field,
        NumberKeyboard,
        Dialog,
        Toast,
        NoticeBar,
        Row,
        Tag,
        popup,
        DatetimePicker,
        ImagePreview,
        CellGroup,
        Uploader,
        Col,
        Calendar,
        Cascader,
        Picker,
        RadioGroup,
        Radio,
        Divider,
        Stepper,
        VTable,
        AddressList,
        Checkbox,
        CheckboxGroup,
    },
    data() {
        return {
            id: "",
            type: "",
            type2: false,
            lock: false,
            //orderList
            showPickerPOrderID: false,
            columnsPOrderIDND: [],
            columnsPOrderID: [],
            showOrder: "",
            searchKey1: "",
            //FACTORY
            showPickerFactoryID: false,
            columnsFactoryIDND: [{ FactoryName: "太仓工厂", FactoryID: "TC00", FactoryIDAndFactoryName: "TC00 太仓工厂" }],
            FactoryID: "TC00",
            showFactory: "TC00 太仓工厂",
            //ShiftID
            showPickerPShiftID: false,
            columnsPShiftIDND: [],
            showShift: "",
            //showMachine
            showMachine: "",
            //showTimePicker
            showTimePicker: false,
            showOTime: null,

            showCTime: null,
            //NoticeMemo
            showAbn: "质量异常",
            showAbnplus: "",
            showNMeno: "",
            status: 0,
            showPickerNoticeMemo: false,
            columnsQProblem: [],
            result: [],
            yj: "",
            pyj: "",
            columnsMachineEquipment: [],

            currentdate: new Date(),
            nowDate: null,
            showProductionDate: false,
            showProDatetimeFrom: false,
            showProDatetimeTo: false,
            showQProblemID: false,
            showPickerpMachineEquipmentID: false,
            user: [],
            form: {
                //申请理由
                MachineID: "",
                PShiftID: "",
                POrderID: "",
                MaterialID: "",
                MaterialName: "",
                OccurDatetime: "",
                CompleteDatetime: "",
                AbnormalTypeID: "",
                AbnormalDescription: "",
                RequestMemo: "",
                AbnormalLevl: "",
                FactoryID: "TC00",
                DataCollectionItemID: "",
                DataCollectionItemName: "",
                FailCode: "",
                LowLevel: 0,
                HighLevel: 0,
                FactValue: 0,
                RealtimeMsgID: 0,
                Creator: "",
                CreateDatetime: "",
                PrdChecked: false,
                PrdAgree: false,
                PrdCheckedDatetime: "",
                PrdCheckedMan: "",
                PrdCheckedMemo: "",
                FactoryManagerChecked: false,
                FactoryManagerAgree: false,
                FactoryManagerCheckedDatetime: "",
                FactoryManagerCheckedMan: "",
                FactoryManagerCheckedMemo: "",
                Token: "",
                item: null
            },
        };
    },
    created: function () {
        this.user = JSON.parse(storage.get("user"));
        this.user.userId = this.user.userName;
    },
    mounted() {
        this.id = this.$ac.pageParam.id
        this.item = this.$ac.pageParam.item;
        //alert(JSON.stringify(this.item));
        // alert( this.item.DataCollectionItemName);
        // let val1 = this.item.DataCollectionItemName.split("质量原因是");
        // let val2 =val1.split(";");
        // this.showAbnplus  = val2[0];
        if (this.$ac.pageParam.type == "01") {
            this.type = "生产部"
        }

        if (this.$ac.pageParam.type == "02") {
            this.type = "质量部"
        }


        if (this.$ac.pageParam.type2 == "01") {
            this.type2 = true
        }

        this.lock = this.$ac.pageParam.lock


        this.getDetail(this.id)

    },

    watch: {
        "form.PrdAgree": {
            handler(newVal, oldavl) {
                if (this.type2) {

                    // this.form.PrdCheckedMemo=this.pyj
                    return
                }
                if (newVal == true) {
                    this.form.PrdCheckedMemo = this.formatDate(new Date()) + " " + this.type + "主管，" + this.user.employeeName + "同意"
                } else {
                    this.form.PrdCheckedMemo = this.formatDate(new Date()) + " " + this.type + "主管，" + this.user.employeeName + "不同意"
                }

            },
            immediate: true
        },
        "form.FactoryManagerAgree": {
            handler(newVal, oldavl) {

                if (this.lock) {

                    this.form.FactoryManagerCheckedMemo = this.yj
                    return
                }

                if (newVal == true) {
                    this.form.FactoryManagerCheckedMemo = this.formatDate(new Date()) + " 利润点负责人，" + this.user.employeeName + "同意"
                } else {
                    this.form.FactoryManagerCheckedMemo = this.formatDate(new Date()) + " 利润点负责人，" + this.user.employeeName + "不同意"
                }

            },
            //  immediate:true
        }
    },

    destroyed() {
        clearInterval(this.nowtimer);
        this.nowDate = null;
    },

    methods: {

        async onSubmit() {
            var _this = this;
            Toast.loading({
                mask: true,
                forbidClick: true,
                duration: 0,
                message: "请稍等...",
            });

            try {
                let res = {}

                this.form.PrdCheckedMan = this.user.userName;
                this.form.PrdChecked = true
                this.form.PrdCheckedDatetime = new Date()
                res = await _httpClient.post(
                    _httpClient.getApiPrefix() + "/ProductionCompromiseRequest/SaveCheck",
                    {
                        body: this.form
                    }
                );


                if (res.Message != null) {
                    //message不为空,有报错信息，打卡失败 暂时这样判断
                    Toast.clear();
                    Toast.fail("保存失败,请核对信息！");
                } else {
                    //打卡成功
                    Toast.clear();
                    Toast.success("保存成功！");


                    if (!this.type2) {
                        if (this.type == "生产部") {
                            this.$ac.sendEvent({ name: "ab-sc" })
                        } else {
                            this.$ac.sendEvent({ name: "ab-zl" })
                        }

                    } else {

                        this.$ac.sendEvent({ name: "ab-sh" })
                    }

                    this.$ac.sendEvent({
                        name: 'flush-list'
                    });


                    setTimeout(function () {
                        _this.$ac.closeWin();
                    }, 2000);
                }
            } catch (e) {

            }
        },

        // 获取当前时间 yy-mm-dd hh:mf:ss
        getNowTimeToSecond() {
            let dateTime;
            let yy = new Date().getFullYear();
            let mm = new Date().getMonth() + 1;
            let dd = new Date().getDate();
            let hh = new Date().getHours();
            let mf =
                new Date().getMinutes() < 10
                    ? "0" + new Date().getMinutes()
                    : new Date().getMinutes();
            let ss =
                new Date().getSeconds() < 10
                    ? "0" + new Date().getSeconds()
                    : new Date().getSeconds();
            dateTime = yy + "-" + mm + "-" + dd + " " + hh + ":" + mf + ":" + ss;
            this.nowDate = dateTime;
            return dateTime;
        },


        // 获取当前时间 yy-mm-dd
        formatDate(date) {


            let year = date.getFullYear()
            let mouth = date.getMonth() + 1
            if (mouth < 10) {
                mouth = '0' + mouth
            }
            let day = date.getDate()
            if (day < 10) {
                day = '0' + day
            }

            var h = date.getHours();
            h = h < 10 ? '0' + h : h;
            var m = date.getMinutes();
            m = m < 10 ? '0' + m : m;
            return year + '-' + mouth + '-' + day + " " + h + ":" + m
        },

        //审核
        async getDetail(id) {
            let _this = this;
            try {

                //ProductionCompromiseRequest/GetEntity
                let res = await _httpClient.get(
                    _httpClient.getApiPrefix() + "/ProductionCompromiseRequest/GetEntity?ID=" + id);
                if (res.HttpCode == 200) {
                    //alert(JSON.stringify(res));
                    _this.form = res.Data
                    _this.yj = _this.form.FactoryManagerCheckedMemo
                    _this.pyj = _this.form.PrdCheckedMemo

                    _this.showOrder = _this.form.POrderID + " " + _this.form.MaterialName
                    //ProductionShift/GetEntityName?FieldValue=BC01

                    _this.form.PrdAgree = true

                    var val1 = _this.form.DataCollectionItemName.split("质量原因是:");
                    //alert(val1[1]);
                    var val2 = val1[1].split(";无");
                    _this.showAbnplus = val2[0];
                    //alert(_this.form.AbnormalTypeID)

                    _this.showAbn = "质量异常";


                    let res2 = await _httpClient.get(_httpClient.getApiPrefix() + "/ProductionShift/GetEntityName?FieldValue=" + this.form.PShiftID)

                    if (res2.HttpCode == 200) {
                        _this.showShift = _this.form.PShiftID + " " + res2.Data
                    }


                    //AbnormalType/GetEntityName
                    let res3 = await _httpClient.get(_httpClient.getApiPrefix() + "/AbnormalType/GetEntityName?FieldValue=" + this.form.AbnormalTypeID)

                    if (res3.HttpCode == 200) {
                        _this.showNMeno = _this.form.AbnormalTypeID + " " + res3.Data
                    }

                    //MachineEquipment/GetEntityName
                    let res4 = await _httpClient.get(_httpClient.getApiPrefix() + "/MachineEquipment/GetEntityName?FieldValue=" + this.form.MachineID)

                    if (res4.HttpCode == 200) {
                        _this.showMachine = _this.form.MachineID + " " + res4.Data
                    }
                    _this.showOTime = _this.form.OccurDatetime;
                    _this.showCTime = this.form.CompleteDatetime
                }


            } catch (e) {
                alert(e)
            }
        },
    },

};
</script>
  
<style>
body {
    background-color: white;
}
</style>
  